<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8" >
            <a-form-model-item label="户名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusName">
              <j-popup
                v-model="model.cusName"
                field="cusName"
                org-fields="id,name"
                dest-fields="customerId,cusName"
                code="arc_customer"
                :multi="true"
                @input="popupCallback"
                />
                <j-input v-model="model.customerId" hidden></j-input>
            </a-form-model-item>      
          </a-col>
        
          <a-col :span="8" >
            <a-form-model-item label="合计金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="alienMoney">
              <a-input-number v-model="model.alienMoney" disabled style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="杂项明细" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="blExpenseSubAlienTable.loading"
          :columns="blExpenseSubAlienTable.columns"
          :dataSource="blExpenseSubAlienTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"
          @valueChange="alientValueChange"
          />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'BlAlienExpenseForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           customerId: [
              { required: true, message: '请输入户号!'},
           ],          
           money: [
              { required: true, message: '请输入合计金额!'},
           ], 
        },
        refKeys: ['blExpenseSubAlien', ],
        tableKeys:['blExpenseSubAlien', ],
        activeKey: 'blExpenseSubAlien',
        // 计费明细
        blExpenseSubAlienTable: {
          loading: false,
          dataSource: [],
          columns: [
          
            {
              title: '费用性质',
              key: 'natureId',
              type: FormTypes.select,
              dictCode:"mws_t_base_natures,name,id,pid !=0",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '价格',
              key: 'price',
              type: FormTypes.select,
              dictCode:"mws_t_base_water_prices,price,id",
              width:"200px",              
              defaultValue:'1.0',
              disabled:true,              
            },            
          
            {
              title: '数量',
              key: 'amount',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: 1,
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '金额',
              key: 'totalMoney',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: 0.0,
              disabled:true,
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },           
            
          ]
        },
        url: {
          add: "/bl/blExpenses/addAlien",
          edit: "/bl/blExpenses/editAlien",
          queryById: "/bl/blExpenses/queryById",
          blExpenseSubAlien: {
            list: '/bl/blExpenses/queryAlienBlSubExpensesByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.blExpenseSubAlienTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      alientValueChange(event){
         const { type, row, column, value, target } = event  
        if( event.column.key=='amount' ){
          let a= event.row.amount;
          let am=[{rowKey: event.row.id, values: {'totalMoney': a }}]
          target.setValues(am)
        }
        this.$refs.blExpenseSubAlien.getValues((error, values) => {
          let tm=0.0;
          for(let i=0; i< values.length;i++ ){
            let row=values[i];
            if(row.amount){
              tm=tm+parseFloat(row.amount) ; 
            }
          }
          this.model.alienMoney=tm;
        }, false)
      },

      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.blExpenseSubAlien.list, params, this.blExpenseSubAlienTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          blSubExpensesList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
       popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      }

    }
  }
</script>

<style scoped>
</style>